﻿package 
{
	import Base.Net.NetSocket;
	
	import Common.GameDefine;
	import Common.GameNetEvent;
	import Common.GameNetManager;
	
	import Events.LogicEvent;
	import Events.LogicEventManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLStream;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import logic.GameCreatureData;
	import logic.GameItem;
	
	import Common.MapDirection;
	import map.MapGrid;
	import map.PathFinder;
	
	/**
	 * ...
	 * @author ...
	 */
	public class GamePlayer extends GameCreature
	{
		public var stone:int = 0;
		
		public var petArray:Array = null;
		public var attackPet:GamePet = null;
		
		public var itemArray:Array = null;
		public var equipArray:Array = null;
		
		////////////////////////////////////////////////////////////////
		// action
		
		
		
		
		
		/////////////////////////////////////////////////////////////////
		
		public function GamePlayer()
		{
			
		}
		
		
		public override function init():void
		{
			super.init();
			
			petArray = new Array();
			itemArray = new Array();
			equipArray = new Array();
		}
		
		
		public override function release():void
		{
			super.release();
			
			attackPet = null;
			
			while( petArray.length )
			{
				petArray[0].release();
				petArray.shift();
			}
			petArray = null;
			
			while( itemArray.length )
			{
				itemArray.shift();
			}
			itemArray = null;
			
			while( equipArray.length )
			{
				equipArray.shift();
			}
			equipArray = null;
		}
		
		
		
		
		public function startAttack():void
		{
			//isAttack = true;
			//isMoving = false;
			
			
		}
		
		
		public function sendDirection():void
		{
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			
			netSocket.writeShort( GameNetEvent.SEND_DIRECTION_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_DIRECTION_ID );
			
			//netManager.writeInt( moveDirection );
			
			netSocket.Send();
		}
			
		
		private function onClick( e:MouseEvent ):void
		{
			var event:LogicEvent = new LogicEvent( LogicEvent.PLAYERCLICK );
			event.object = this;
			
			LogicEventManager.instance.dispatchEvent( event );
		}
		
		
		/////////////////////////////////////////////////////////////////
		public function levelUpPoint( type:int ):void
		{
			data.LevelUpPoint--;
			
			switch ( type )
			{
				case GameCreatureData.ATTR_VITALITY:
					{
						data.Vitality++;
						data.MAXHP += 4;
					}
				break;
				case GameCreatureData.ATTR_STRENGTH:
					{
						data.Attack++;
						data.Strength++;
						data.MAXHP += 1;
					}
				break;
				case GameCreatureData.ATTR_TOUGHNESS:
					{
						data.Defence++;
						data.Toughness++;
						data.MAXHP += 1;
					}
				break;
				case GameCreatureData.ATTR_DEXTERITY:
					{
						data.Quick++;
						data.Dexterity++;
						data.MAXHP += 1;
					}
				break;
			}
			
			
		}
		
		
		public function getLevelUpPoint():int
		{
			return data.LevelUpPoint;
		}
		
		
		
		/////////////////////////////////////////////////////////////////
		
		public function addPet( p:GamePet ):void
		{
			petArray.push( p );
		}
		
		
		/////////////////////////////////////////////////////////////////
		
		
		public function addItem( item:GameItem ):void
		{
			itemArray[ item.i ] = item;
		}
		
		public function removeItem( i:int ):void
		{
			itemArray[ i ] = null;
		}
		
		public function getItem( i:int ):GameItem
		{
			return itemArray[ i ];
		}
		
		public function addEquip( item:GameItem ):void
		{
			equipArray[ item.i ] = item;
		}
		
		public function getEquip( i:int ):GameItem
		{
			return equipArray[ i ];
		}
		
		public function removeEquip( i:int ):void
		{
			equipArray[ i ] = null;
		}
		
		public function equipItem( i1:int , i2:int ):void
		{
			var temp:GameItem = itemArray[ i1 ];
			itemArray[ i1 ] = equipArray[ i2 ];
			equipArray[ i2 ] = temp;
			
			if ( itemArray[ i1 ] )
			{
				itemArray[ i1 ].i = i1;
				itemArray[ i1 ].item = 1;
			}
				
			if ( equipArray[ i2 ] )
			{
				equipArray[ i2 ].i = i2;
				equipArray[ i2 ].item = 0;
			}
		}
		
		public function switchItem( i1:int , i2:int ):void
		{
			var temp:GameItem = itemArray[ i1 ];
			itemArray[ i1 ] = itemArray[ i2 ];
			itemArray[ i2 ] = temp;
			
			if ( itemArray[ i1 ] )
				itemArray[ i1 ].i = i1;
			
			if ( itemArray[ i2 ] )
				itemArray[ i2 ].i = i2;
		}
		
		public function switchEquip( i1:int , i2:int ):void
		{
			var temp:GameItem = equipArray[ i1 ];
			equipArray[ i1 ] = equipArray[ i2 ];
			equipArray[ i2 ] = temp;
			
			if ( equipArray[ i1 ] )
				equipArray[ i1 ].i = i1;
			
			if ( equipArray[ i2 ] )
				equipArray[ i2 ].i = i2;
		}
		
		public function canEquipItem( i:int , p:int ):int
		{
			var item:GameItem = itemArray[ i ];
			
			switch( item.type )
			{
				case GameItem.IT_CLAW:
				case GameItem.IT_AXE:
				case GameItem.IT_STICK:
				case GameItem.IT_SPEAR:
				case GameItem.IT_BOW:
				{
					return GameItem.ET_RIGHT;
				}
				break;
				case GameItem.IT_HELMET:
				{
					return GameItem.ET_HEAD;
				}
				break;
				case GameItem.IT_ARMOR:
				{
					return GameItem.ET_BODY;
				}
				break;
				case GameItem.IT_BRACELET:
				case GameItem.IT_FLUTE:
				case GameItem.IT_JEWELRY:
				case GameItem.IT_RING:
				case GameItem.IT_EARRING:
				case GameItem.IT_AMULET:
				{
					if ( p == GameItem.ET_RING1 || p == GameItem.ET_RING2 )
						return p;
				}
				break;
				
				
			}
			
			return GameItem.ET_COUNT;
		}
		
		public function clearItem( i:int ):void
		{
			var item:GameItem = itemArray[ i ];
			item = null;
		}
		
		public function useItem( i:int , p:int ):int
		{
			var item:GameItem = itemArray[ i ];
			
			switch( item.type )
			{
				case GameItem.IT_CLAW:
				case GameItem.IT_AXE:
				case GameItem.IT_STICK:
				case GameItem.IT_SPEAR:
				case GameItem.IT_BOW:
				{
					equipItem( i , GameItem.ET_RIGHT );
					return GameItem.ET_RIGHT;
				}
				break;
				case GameItem.IT_HELMET:
				{
					equipItem( i , GameItem.ET_HEAD );
					return GameItem.ET_HEAD;
				}
				break;
				case GameItem.IT_ARMOR:
				{
					equipItem( i , GameItem.ET_BODY );
					return GameItem.ET_BODY;
				}
				break;
				case GameItem.IT_BRACELET:
				case GameItem.IT_FLUTE:
				case GameItem.IT_JEWELRY:
				case GameItem.IT_RING:
				case GameItem.IT_EARRING:
				case GameItem.IT_AMULET:
				{
					if ( p != GameDefine.INVALIDID )
					{
						equipItem( i , p );
						return p;
					}
					
					if ( !itemArray[ GameItem.ET_RING1 ] )
					{
						equipItem( i , GameItem.ET_RING1 );
						return GameItem.ET_RING1;
					}
					else if ( !itemArray[ GameItem.ET_RING2 ] )
					{
						equipItem( i , GameItem.ET_RING2 );
						return GameItem.ET_RING2;
					}
					else
					{
						equipItem( i , GameItem.ET_RING1 );
						return GameItem.ET_RING1;
					}
				}
				break;
				case GameItem.IT_FOOD:
				case GameItem.IT_GOOD:
					
					if ( p != GameDefine.INVALIDID )
					{
						item.num--;
					}
					
				break;
				
			}
			
			return GameItem.ET_COUNT;
		}
		
		public function dropEquip( i1:int , i2:int ):void
		{
			equipItem( i2 , i1 );
		}
		
		/////////////////////////////////////////////////////////////////

		public function heal( mode:int ):void
		{
			switch ( mode )
			{
				case GameNPC.HEALALL:
				{
					data.HP = data.MAXHP;
					
					for ( var i:int = 0 ; i < petArray.length ; i++ )
					{
						petArray[i].data.HP = petArray[i].data.MAXHP;
					}
				}
				break;
				case GameNPC.HEALSELF:
				{
					data.HP = data.MAXHP;
				}
				break;
				case GameNPC.HEALPET:
				{
					for ( i = 0 ; i < petArray.length ; i++ )
					{
						petArray[i].data.HP = petArray[i].data.MAXHP;
					}
				}
				break;
			}
		}
		
		
	}
	
}